//
//  SN_Question_ViewController.m
//  Shiyun_Newspaper
//
//  Created by Sylar on 2017/6/5.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_Question_ViewController.h"

#import "SN_Question_TableViewCell.h"

#import "SN_NewspaperModel.h"

#import "SNSearchBar.h"

#import "SN_QuestionReadMoreNews_Controller.h"

#import "SN_QuestionBgAlertView.h"

#import "SN_Question_ResolveController.h"

#import "SN_SearchViewController.h"

#import "ScanImageViewController.h"

#import "SN_QuestionReadMore_Picker.h"

//模型类
#import "SN_QuesList.h"
#import "SN_AlertModel.h"
#import "SN_NewspaperModel.h"
#import "UIView+WebCache.h"

@interface SN_Question_ViewController () <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,SN_QuestionAlertDelegate>
{
    NSString *defaultGrader;
    NSString *defaultEdition;
    
    NSInteger defaultSelectedEditionIndex;
    NSInteger selectedEditionIndex;
    NSInteger selectedGradeIndex;
}

@property (nonatomic, strong) UIButton *subjectBtn;
@property (nonatomic, strong) UIControl *control;
@property (nonatomic, strong) UIImage *arrowImage;
@property (nonatomic, strong) UIButton *scanBtn;
@property (nonatomic, strong) SNSearchBar *snSearch;

@property (nonatomic, strong) UITableView *sel_edition;
@property (nonatomic, strong) UITableView *sel_grader;
@property (nonatomic, strong) NSMutableArray *graderArr;
@property (nonatomic, strong) NSMutableArray *editionArr;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;

//@property (nonatomic, strong) NSIndexPath *lastSelectIndexPath; //上次选中的cell

@property (nonatomic, copy) NSString *editionTypeStr;  //所选版本
@property (nonatomic, copy) NSString *gradeStr;  //所选年级

@property (nonatomic, copy) NSString *editionTypeIdStr;  //所选版本
@property (nonatomic, copy) NSString *gradeIdStr;  //所选年级

@property (nonatomic, strong) UILabel *emptyLabel;

@property (nonatomic, strong) UIPickerView *picker;

@end

@implementation SN_Question_ViewController {

    BOOL isDown;
}

#pragma mark -LifeCycle

- (instancetype)init {
    
    if (self == [super init]) {
        self.hidesBottomBarWhenPushed = NO;
        isDown = true;
    }
    
    
    return self;
}

- (void)loadView {
    
    [super loadView];
    
    //获取所选报纸版本与年级
    _gradeStr = [SYAccountTool ShareAccount].account.gradername;
    _editionTypeStr = [SYAccountTool ShareAccount].account.edtiontypename;
    
    _gradeIdStr = [SYAccountTool ShareAccount].account.grader;
    _editionTypeIdStr = [SYAccountTool ShareAccount].account.edtiontype;
    
    selectedEditionIndex = 0;
    defaultSelectedEditionIndex = 0;
    
    
    defaultGrader = _gradeStr;
    defaultEdition = _editionTypeStr;
    
    if (!_gradeStr) _gradeStr = @"一年级";
    if (!_editionTypeStr) _editionTypeStr = @"人教版";
    if (!_gradeIdStr) _gradeIdStr = @"1";
    if (!_editionTypeIdStr) _editionTypeIdStr = @"1";
    
    NSString *btnTitle = [NSString stringWithFormat:@"%@ · %@",_editionTypeStr,_gradeStr];
    
    [self.subjectBtn setTitle:btnTitle forState:UIControlStateNormal];

    self.snSearch.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    [[DBUtil shareInstance] getEditionAndGradeData:^(NSError *error, NSDictionary *editionInfo, NSDictionary *gradeInfo) {
        
        self.graderArr = [gradeInfo objectForKey:@"re_data"];
        self.editionArr = [editionInfo objectForKey:@"re_data"];
        
        for (int i = 0; i < self.editionArr.count; i++) {
            
            if ([_editionTypeStr isEqualToString:[[self.editionArr objectAtIndex:i] objectForKey:@"editiontypename"]]) {
                defaultSelectedEditionIndex = i;
                selectedEditionIndex = i;
                break;
            }
        }
    }];
    
    //请求数据
    [self requestListDataWithHud:YES];
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self requestListDataWithHud:NO];
}

#pragma mark -Getter

- (UIButton *)subjectBtn {
    
    if (_subjectBtn == nil) {
        
        _subjectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_subjectBtn addTarget:self action:@selector(selectEdition:) forControlEvents:(UIControlEventTouchUpInside)];
        [_subjectBtn setFrame:CGRectMake(15, SafeAreaTopHeight - 18 - 13, 134, 18)];
        _subjectBtn.adjustsImageWhenHighlighted = NO;
        
        [_subjectBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [_subjectBtn setTitleColor:colorFromHex(@"#ffffff") forState:UIControlStateNormal];
        [_subjectBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
        [_subjectBtn setImage:[UIImage imageNamed:@"choose_Down"] forState:(UIControlStateNormal)];
        [_subjectBtn setImage:[UIImage imageNamed:@"choose_Up"] forState:(UIControlStateSelected)];
        [_subjectBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 112, 0, 0)];
        
        [self.navBar addSubview:_subjectBtn];
    }
    return _subjectBtn;
}

- (SNSearchBar *)snSearch {
    
    if (_snSearch == nil) {
        
        _snSearch = [[SNSearchBar alloc]initWithFrame:CGRectMake(self.subjectBtn.right, SafeAreaTopHeight - 28 - 8, SCREENWIDTH-30-10-self.subjectBtn.width, 28) leftImage:[UIImage imageNamed:@"search_Img"] placeholderColor:colorFromHex(@"fb524a")];
        _snSearch.hasCentredPlaceholder = NO;
        _snSearch.delegate = self;
        _snSearch.layer.cornerRadius = 4;
        _snSearch.layer.masksToBounds = YES;
        [_snSearch setPlaceholder:@"搜索"];
        [_snSearch setBackgroundColor:[UIColor whiteColor]];
        [_snSearch setBackgroundImage:[Utils imageWithColor:[UIColor clearColor] size:_snSearch.bounds.size]];
        [self.navBar addSubview:_snSearch];
        
        self.scanBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self.scanBtn setFrame:CGRectMake(_snSearch.width - 28, 0, 28, 28)];
        [self.scanBtn addTarget:self action:@selector(scanAction) forControlEvents:(UIControlEventTouchUpInside)];
        [self.scanBtn setImage:[UIImage imageNamed:@"scanBtn"] forState:(UIControlStateNormal)];
        
        [_snSearch addSubview:self.scanBtn];
    }
    return _snSearch;
}

- (UIControl *)control {
    
    if (_control == nil) {
        
        _control = [[UIControl alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH-SafeAreaTopHeight-45)];
        _control.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"control_bg"]];
        
        [_control addTarget:self action:@selector(hideControl) forControlEvents:UIControlEventTouchUpInside];
    }
    return _control;
}

- (UITableView *) sel_grader {
    
    if (_sel_grader==nil) {
        
        _sel_grader =[[UITableView alloc]initWithFrame:CGRectMake(SCREENWIDTH/2, 0, SCREENWIDTH/2, 360) style:UITableViewStylePlain];
        _sel_grader.delegate=self;
        _sel_grader.dataSource=self;
        _sel_grader.scrollEnabled = NO;
        _sel_grader.separatorStyle= UITableViewCellSeparatorStyleNone;
        _sel_grader.rowHeight = 40.0;
        
    }
    
    return _sel_grader;
}


- (UITableView *) sel_edition {
    
    if (_sel_edition==nil) {
        
        _sel_edition =[[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREENWIDTH/2, 360) style:UITableViewStylePlain];
        _sel_edition.delegate=self;
        _sel_edition.dataSource=self;
        _sel_edition.scrollEnabled = NO;
        _sel_edition.rowHeight = 40.0;
        _sel_edition.separatorStyle= UITableViewCellSeparatorStyleNone;
        _sel_edition.backgroundColor=[UIColor colorWithHexString:@"f4f5f7"];
    }
    
    return _sel_edition;
}

- (UIImage *)arrowImage {
    
    if (!_arrowImage) {
        
        _arrowImage = [UIImage imageNamed:@"choose_Down"];
    }
    return _arrowImage;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, VIEW_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight - 49) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        [_tableView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
           
            [self requestListDataWithHud:NO];
        }];
        
        _emptyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, 100)];
        _emptyLabel.font = [UIFont systemFontOfSize:15];
        _emptyLabel.text = @"暂无数据。";
        _emptyLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        _emptyLabel.textAlignment = NSTextAlignmentCenter;
        _emptyLabel.hidden = YES;
        _emptyLabel.center = self.view.center;
        [_tableView addSubview:_emptyLabel];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArr {
    
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
    }
    
    return _dataArr;
}

#pragma mark -Private

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (tableView == self.tableView) {
        return self.dataArr.count;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.sel_grader) {
        return self.graderArr.count;
    }else if (tableView == self.sel_edition) {
        return self.editionArr.count;
    }else if (tableView == self.tableView) {
        SN_QuesList *model = self.dataArr[section];
        
        return model.szblist.count;
    }
    return 0;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.tableView) {
        static NSString *cellID = @"cellid";
        
        SN_Question_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            
            cell = [[SN_Question_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        SN_QuesList *listModel = self.dataArr[indexPath.section];
        
        SN_NewspaperModel *model = listModel.szblist[indexPath.row];
        
        if (indexPath.row == 0) {
            
            model.isFirstIndex = YES;
        }
        
        cell.model = model;
        
        if (model.isread == 0) {
            cell.titleLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
            [cell.issueBtn setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateNormal];
        }else {
            cell.titleLabel.textColor = [UIColor colorWithHexString:@"#afafaf"];
            [cell.issueBtn setTitleColor:[UIColor colorWithHexString:@"#afafaf"] forState:UIControlStateNormal];
        }

        
        return cell;
    }else if (tableView == self.sel_edition) {
        
        UITableViewCell *editionCell = [tableView dequeueReusableCellWithIdentifier:@"editionIdentifier"];
        
        if (editionCell == nil) {
            editionCell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"editionIdentifier"];
            
            editionCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        for (UIView *view in editionCell.contentView.subviews) {
            if (view.tag > 60) {
                [view removeFromSuperview];
            }
        }
        
        NSDictionary *dict = [self.editionArr objectAtIndex:indexPath.row];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH/2, 40)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.tag = 61;
        [titleLabel setTextColor:colorFromHex(@"#3b3b3b")];
        [titleLabel setFont:[UIFont systemFontOfSize:16]];
        [titleLabel setText:[dict objectForKey:@"editiontypename"]];
        [editionCell.contentView addSubview:titleLabel];
        
        if ([[dict objectForKey:@"editiontypename"] isEqualToString:defaultEdition]) {
            [titleLabel setTextColor:colorFromHex(@"#fb524a")];
            [editionCell setBackgroundColor:[UIColor whiteColor]];
        }else {
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(SCREENWIDTH/2-0.5, 0, 0.5, 40)];
            [line setBackgroundColor:colorFromHex(@"#dedede")];
            line.tag = 62;
            [editionCell.contentView addSubview:line];
            [editionCell setBackgroundColor:colorFromHex(@"#f4f5f7")];
        }
        
        return editionCell;
        
    }else if (tableView == self.sel_grader) {
        
        UITableViewCell *graderCell = [tableView dequeueReusableCellWithIdentifier:@"graderIdentifier"];
        
        if (graderCell == nil) {
            graderCell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"graderIdentifier"];
            graderCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        for (UIView *view in graderCell.contentView.subviews) {
            if (view.tag > 50) {
                [view removeFromSuperview];
            }
        }
        
        NSDictionary *dict = [self.graderArr objectAtIndex:indexPath.row];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH/2, 40)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.tag = 60;
        if ([[dict objectForKey:@"gradername"] isEqualToString:defaultGrader]) {
            [titleLabel setTextColor:colorFromHex(@"#fb524a")];
        }else {
            [titleLabel setTextColor:colorFromHex(@"#3b3b3b")];
        }
        
        if (defaultGrader == nil && indexPath.row == 0) {
            [titleLabel setTextColor:colorFromHex(@"#fb524a")];
        }
        
        [titleLabel setFont:[UIFont systemFontOfSize:16]];
        [titleLabel setText:[dict objectForKey:@"gradername"]];
        
        [graderCell.contentView addSubview:titleLabel];
        
        return graderCell;
    }
    
    return nil;

}

#pragma mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 24 + 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (self.dataArr.count == 0) {
        
        return nil;
    }
    
    if (tableView == self.tableView) {
        SN_QuesList *model = self.dataArr[section];
        
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 41)];
        headerView.backgroundColor = [UIColor whiteColor];
        
        //标题label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 14, 100, 16)];
        titleLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        titleLabel.font = [UIFont systemFontOfSize:16];
        titleLabel.text = model.coursename;
        [headerView addSubview:titleLabel];
        
        //一条横线
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, VIEW_WIDTH - 15, 0.5)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [headerView addSubview:lineView];
        
        //更多的按钮
        UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [moreBtn setTitle:@"更多" forState:UIControlStateNormal];
        moreBtn.tag = section + 100;
        moreBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [moreBtn setTitleColor:[UIColor colorWithHexString:@"#bebebe"] forState:UIControlStateNormal];
        moreBtn.frame = CGRectMake(VIEW_WIDTH - 15 - 38, 14, 38, 33);
//        moreBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        moreBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
        moreBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [moreBtn addTarget:self action:@selector(readMoreAction:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:moreBtn];
        
        if (section == 0) {
            
            lineView.hidden = YES; //第一个section隐藏线条
        }
        
        return headerView;
    }
    
    return nil;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (tableView == self.tableView) {
        return 41;
    }
    return 0;
}

#pragma mark -TableView点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView == self.tableView) {
        SN_Question_ResolveController *vc = [[SN_Question_ResolveController alloc] init];
        SN_QuesList *listModel = self.dataArr[indexPath.section];
        SN_NewspaperModel *newsModel = listModel.szblist[indexPath.row];
        if (newsModel.isread == 0) {
            
            newsModel.isread = 1;
            [[listModel.szblist mutableCopy] replaceObjectAtIndex:indexPath.row withObject:newsModel];
            [self.dataArr replaceObjectAtIndex:indexPath.section withObject:listModel];
            [self.tableView reloadData];
        }
        
        
        vc.newsId = [NSString stringWithFormat:@"%ld",(long)newsModel.newid];
        vc.titleStr = newsModel.newsname;
        vc.editionIdStr = [NSString stringWithFormat:@"%ld期",(long)newsModel.phase];
        
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }else if (tableView == self.sel_edition) {
        
        if (defaultSelectedEditionIndex != indexPath.row) {
            defaultGrader = nil;
            [self.sel_grader reloadData];
        }
        defaultSelectedEditionIndex = indexPath.row;
        defaultEdition = [[self.editionArr objectAtIndex:indexPath.row] objectForKey:@"editiontypename"];;
        
        [self.sel_edition reloadData];
        
        
    }else if (tableView == self.sel_grader) {
        
        selectedEditionIndex = defaultSelectedEditionIndex;
        selectedGradeIndex = indexPath.row;
        _gradeIdStr = [NSString stringWithFormat:@"%@",[[self.graderArr objectAtIndex:indexPath.row] objectForKey:@"gradersid"]];
        _editionTypeIdStr = [NSString stringWithFormat:@"%@",[[self.editionArr objectAtIndex:selectedEditionIndex] objectForKey:@"editiontypeid"]];
        _gradeStr = [[self.graderArr objectAtIndex:indexPath.row] objectForKey:@"gradername"];
        _editionTypeStr = [[self.editionArr objectAtIndex:selectedEditionIndex] objectForKey:@"editiontypename"];
        
        //储存到本地
        NSDictionary *imformation = [SyUserInfomation acquirePersonalImformationFromMemoryWithPersonID:USERID];
        NSMutableDictionary *informationM = [NSMutableDictionary dictionaryWithDictionary:imformation];
        
        [informationM setObject:_gradeIdStr forKey:@"grader"];
        [informationM setObject:_editionTypeIdStr forKey:@"edtiontype"];
        [informationM setObject:_editionTypeStr forKey:@"edtiontypename"];
        [informationM setObject:_gradeStr forKey:@"gradername"];
        
        [SyUserInfomation personalImformationSaveWtih:informationM];
        
        //按钮内容
        NSString *subStr = [NSString stringWithFormat:@"%@ · %@",[[self.editionArr objectAtIndex:selectedEditionIndex] objectForKey:@"editiontypename"],[[self.graderArr objectAtIndex:indexPath.row] objectForKey:@"gradername"]];
        [self.subjectBtn setTitle:subStr forState:(UIControlStateNormal)];
        
        //刷新数据
        [self requestListDataWithHud:YES];
        
        [self hideControl];
    }

}

#pragma mark -Network
- (void)requestListDataWithHud:(BOOL)showHud {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:@"api_szb_doquesionlist" forKey:@"method"];
    [dict setObject:_editionTypeIdStr forKey:@"editiontype"];
    [dict setObject:_gradeIdStr forKey:@"gradersid"];
    
    __weak typeof(self) weakSelf = self;
    [Utils getWithparameters:dict showHUD:showHud success:^(NSInteger recode, id jsonObject) {
        
        if ([jsonObject isKindOfClass:[NSDictionary class]]) {
            
            NSArray *re_dataArr = [jsonObject objectForKey:@"re_data"];
            
            [self.tableView.mj_header endRefreshing];

            if (re_dataArr.count == 0) {  //空处理
                
                weakSelf.dataArr = nil;
                [weakSelf.tableView reloadData];
                weakSelf.emptyLabel.hidden = NO;
                return;
            }
            
            weakSelf.emptyLabel.hidden = YES;
            weakSelf.dataArr = [SN_QuesList mj_objectArrayWithKeyValuesArray:re_dataArr];
            
            [weakSelf.tableView reloadData];

        }
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

#pragma mark -UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
    NSLog(@"搜索");
    SN_SearchViewController *searchVC = [[SN_SearchViewController alloc]init];
    searchVC.gradersId = self.gradeIdStr;
    searchVC.editionType = self.editionTypeIdStr;
    searchVC.editionName = self.editionTypeStr;
    searchVC.gradersName = self.gradeStr;
    searchVC.searchType = 1;
    
    SYNavigationController *nav = [[SYNavigationController alloc]initWithRootViewController:searchVC];
    
    [self presentViewController:nav animated:YES completion:nil];
    return NO;
}

#pragma mark -SN_QuestionAlertDelegate 
- (void)confirmWithYear:(NSString *)year andEditionType:(NSString *)editionType {
    
//    SN_QuestionReadMoreNews_Controller *vc = [[SN_QuestionReadMoreNews_Controller alloc] init];
//    vc.
}

#pragma mark -Action

//点击回收键盘
- (void)tapAction {
    
    [self.view endEditing:YES];
}

//选择年级和期数
- (void)selectEdition:(UIButton *)sender {
    
    if (_control) {
        [self hideControl];
    }else {
        
        float angle = isDown ? -M_PI : 2*M_PI;
        
        isDown = !isDown;
        [UIView animateWithDuration:0.3 animations:^{
            
            self.subjectBtn.imageView.transform = CGAffineTransformMakeRotation(angle);
        }];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(SCREENWIDTH/2-0.5, 40*self.editionArr.count, 0.5, 40*(9-self.editionArr.count))];
        [line setBackgroundColor:colorFromHex(@"#DEDEDE")];
        [self.sel_edition addSubview:line];
        [self.control addSubview:self.sel_edition];
        [self.control addSubview:self.sel_grader];
        
        [self.view addSubview:self.control];
    }
    
}

- (void)hideControl {
    
    float angle = isDown ? -M_PI : 2*M_PI;
    
    isDown = !isDown;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.subjectBtn.imageView.transform = CGAffineTransformMakeRotation(angle);
    }];

    
    self.subjectBtn.selected = NO;
    [self.control removeFromSuperview];
    self.control = nil;
    self.sel_grader = nil;
    self.sel_edition = nil;
    
    defaultGrader = _gradeStr;
    defaultEdition = _editionTypeStr;
    
    defaultSelectedEditionIndex = selectedEditionIndex;
}


//扫描
- (void)scanAction {
    
    ScanImageViewController *scan = [[ScanImageViewController alloc] init];
    //scan.delegate = self;
    scan.type = 2;
    [self.navigationController pushViewController:scan animated:YES];
    
}

//查看更多
- (void)readMoreAction:(UIButton *)sender {
    
    NSInteger index = sender.tag - 100;
    
    SN_QuesList *model = [self.dataArr objectAtIndex:index];

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"api_szb_yearphaselist" forKey:@"method"];
    [dict setObject:_editionTypeIdStr forKey:@"editiontype"];
    [dict setObject:_gradeIdStr forKey:@"gradersid"];
    [dict setObject:model.courseid forKey:@"courseid"];
    
    
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        NSMutableArray *dataArr = [SN_AlertModel mj_objectArrayWithKeyValuesArray:arr];
        
        SN_QuestionReadMore_Picker *picker = [[SN_QuestionReadMore_Picker alloc] initWithSuperView:nil];
        picker.courseStr = model.coursename;
        
        NSMutableArray *newArr = [NSMutableArray arrayWithArray:dataArr];
        
        for (int i = 0; i < dataArr.count; i++) { //过滤空数据
            
            SN_AlertModel *model = [dataArr objectAtIndex:i];
            
            if (model.phaselist.count == 0) {
                
                [newArr removeObject:model];
            }
        }
        
        if (newArr.count == 0) {
            [self showHint:@"暂无更多报纸"];
            return;
        }
        
        picker.dataArr = newArr;
        picker.block = ^(SN_NewspaperModel *model,SN_AlertModel *alertModel) {
        
            SN_Question_ResolveController *vc = [[SN_Question_ResolveController alloc] init];
            
            vc.model = model;
            vc.alertModel = alertModel;
            
            [self.navigationController pushViewController:vc animated:YES];
            
        };
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];

    
    return;
    
//    NSInteger index = sender.tag - 100;
//    
//    SN_QuesList *model = [self.dataArr objectAtIndex:index];
//    
//
//    NSString *title = [NSString stringWithFormat:@"%@%@%@",_editionTypeStr,_gradeStr,model.coursename];
//    
//    [SN_QuestionBgAlertView alertWithTitle:title gradeId:_gradeIdStr editionId:_editionTypeIdStr courseId:model.courseid parentView:self.view];
//    
//    return;
    
//    SN_QuestionReadMoreNews_Controller *vc = [[SN_QuestionReadMoreNews_Controller alloc] init];
//    
//    vc.subjectTitle = model.coursename;
//    
//    vc.editionStr = [NSString stringWithFormat:@"%@%@",_gradeStr,model.coursename];
//    
//    [self.navigationController pushViewController:vc animated:YES];
}

@end
